//
//  HYViewController.m
//  HaiYue
//
//  Created by skyline on 2017/12/15.
//  Copyright © 2017年 skyline. All rights reserved.
//

#import "HYViewController.h"
@interface HYViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) HYHud *hud;
@property (nonatomic, strong) HYLabel *titleLBL;
@end

@implementation HYViewController
- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.style = HYViewNaviBarStyleDefault;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)setStyle:(HYViewNaviBarStyle)style
{
    if (_style != style)
    {
        _style = style;
        if (_style == HYViewNaviBarStyleDefault)
        {
            self.leftNaviBarBtn.hidden = YES;
            self.rightNaviBarBtn.hidden = YES;
        }
        else if (_style == HYViewNaviBarStyleOnlyLeftBtn)
        {
            self.leftNaviBarBtn.hidden = NO;
        }
        else if (_style == HYViewNaviBarStyleOnlyRightBtn)
        {
            self.rightNaviBarBtn.hidden = NO;
        }
        else if (_style == HYViewNaviBarStyleLeftAndRightBtn)
        {
            self.leftNaviBarBtn.hidden = NO;
            self.rightNaviBarBtn.hidden = NO;
        }
        else if(_style == HYViewNaviBarStyleSeg)
        {
            self.navigationItem.titleView = self.segNaviBar;
            _leftNaviBarBtn.hidden = YES;
            _search.hidden = YES;
        }
        else if (_style == HYViewNaviBarStyleSearch)
        {
            self.search.hidden = NO;
            self.leftNaviBarBtn.hidden = NO;
            self.rightNaviBarBtn.hidden = NO;
            _segNaviBar.hidden = YES;
        }
    }
}

- (UIButton *)leftNaviBarBtn
{
    if (!_leftNaviBarBtn)
    {
        _leftNaviBarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _leftNaviBarBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        // 让按钮的内容往左边偏移10
//        _leftNaviBarBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
//        _leftNaviBarBtn.backgroundColor = [UIColor redColor];
        _leftNaviBarBtn.bounds = CGRectMake(0, 0, 40, 40);
        [_leftNaviBarBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        [_leftNaviBarBtn setImage:[UIImage imageNamed:@"backSelected"] forState:UIControlStateHighlighted];
        [_leftNaviBarBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_leftNaviBarBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
        [_leftNaviBarBtn addTarget:self action:@selector(actionForLeftNaviBar) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:_leftNaviBarBtn];
    }
    return _leftNaviBarBtn;
}

- (UIButton *)rightNaviBarBtn
{
    if (!_rightNaviBarBtn)
    {
        _rightNaviBarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightNaviBarBtn.bounds = CGRectMake(0, 0, 40, 40);
        _rightNaviBarBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_rightNaviBarBtn setImage:[UIImage imageNamed:@"main_search_selecter"] forState:UIControlStateNormal];
        [_rightNaviBarBtn setImage:[UIImage imageNamed:@"main_search_normal"] forState:UIControlStateHighlighted];
        [_rightNaviBarBtn addTarget:self action:@selector(actionForRightNaviBar) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:_rightNaviBarBtn];
    }
    return _rightNaviBarBtn;
}

- (HYSeg *)segNaviBar
{
    if (!_segNaviBar)
    {
        _segNaviBar = [[HYSeg alloc]initWithItems:@[@"网游",@"H5游戏"]];
        _segNaviBar.style = HYSegStyleCircle;
        _segNaviBar.backgroundColor = RGBA(210, 20, 60, 1);
        _segNaviBar.titleColor = [UIColor whiteColor];
        _segNaviBar.selectedtTitleColor = RGBA(210, 20, 60, 1);
        _segNaviBar.selectedItemColor = [UIColor whiteColor];
        _segNaviBar.bounds = CGRectMake(0.0, 0.0, SCREEN_WIDTH - LeftToRightGap, TitleViewHeight);
        [_segNaviBar addTarget:self action:@selector(actionForSegNaviBar:) forControlEvents:UIControlEventValueChanged];
    }
    return _segNaviBar;
    
}

- (HYTextField *)search
{
    if (!_search)
    {
        self.style = HYViewNaviBarStyleSearch;
        _search = [[HYTextField alloc] init];
        _search.type = HYTextFieldTypeSearch;
        _search.backgroundColor = [UIColor whiteColor];
        _search.alpha = 0.7;
        _search.tintColor = _search.textColor;
        _search.bounds = CGRectMake(0, 0, SCREEN_WIDTH - LeftToRightGap, TitleViewHeight);
        _search.layer.masksToBounds = YES;
        _search.layer.cornerRadius = TitleViewHeight / 2.0;
        _search.font = [UIFont systemFontOfSize:DefaultFont];
        _search.textAlignment = NSTextAlignmentCenter;
        self.navigationItem.titleView = _search;
    }
    return _search;
}

- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        _scrollView = [[UIScrollView alloc]init];
        _scrollView.pagingEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.bounces = NO;
        _scrollView.delegate = self;
        _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * _segNaviBar.items.count, SCREEN_HEIGHT - NAVIBAR_HEIGHT - STATUS_HEIGHT - TABBAR_HEIGHT);
        [self.view addSubview:_scrollView];
        [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsZero);
        }];
    }
    return _scrollView;
}

- (HYLabel *)centerTipsLBL
{
    if (!_centerTipsLBL)
    {
       _centerTipsLBL = [[HYLabel alloc] initWithText:@"" textColor:TEXT_GRAY_COLOR textFont:[UIFont systemFontOfSize:ContentFont]];
        [self.view addSubview:_centerTipsLBL];
        [_centerTipsLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.view);
        }];
    }
    return _centerTipsLBL;
}

//- (HYLabel *)titleLBL
//{
//    if (!_titleLBL)
//    {
//        _titleLBL = [[HYLabel alloc] initWithText:@"" textColor:TEXT_GRAY_COLOR textFont:[UIFont systemFontOfSize:ContentFont]];
//        [self.view addSubview:_centerTipsLBL];
//        [_titleLBL mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.center.equalTo(self.view);
//        }];
//    }
//    return _centerTipsLBL;
//}
- (HYHud *)hud
{
    if (!_hud)
    {
        _hud = [HYHud hud];
        _hud.color = THEME_COLOR;

    }
    return _hud;
}

- (void)startHUD
{
    [self.hud startAnimating];
}

- (void)endHUD
{
    [self.hud stopAnimating];
}

- (void)actionForLeftNaviBar
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)actionForRightNaviBar
{
    
}

- (void)actionForSegNaviBar:(HYSeg*)sender
{
    _scrollView.contentOffset = CGPointMake(SCREEN_WIDTH * sender.currentIndex, 0);
//    self.currentIndex = sender.currentIndex;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (_segNaviBar.hidden == NO && _segNaviBar)
    {
        _segNaviBar.currentIndex = scrollView.contentOffset.x / SCREEN_MIN_LENGTH;
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    [self.view endEditing:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],
                                                                      NSFontAttributeName:[UIFont systemFontOfSize:18]}]; //导航栏标题 字体颜色->白色 ->大小
    //    if (_segNaviBar.hidden == NO && _segNaviBar.items > 0)
    //    {
    //        self.scrollView.hidden = NO;
    //    }
}


- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    [super viewWillDisappear:animated];
    [self endHUD];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
